home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C16 / Recycle.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  3.1 KB  |  131 lines

  1. //: C16:Recycle.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Containers & polymorphism
  7. #include "TStack.h"
  8. #include <fstream>
  9. #include <cstdlib>
  10. #include <ctime>
  11. using namespace std;
  12. ofstream out("recycle.out");
  13.  
  14. enum TrashType { AluminumT, PaperT, GlassT };
  15.  
  16. class Trash {
  17.   float _weight;
  18. public:
  19.   Trash(float wt) : _weight(wt) {}
  20.   virtual TrashType trashType() const = 0;
  21.   virtual const char* name() const = 0;
  22.   virtual float value() const = 0;
  23.   float weight() const { return _weight; }
  24.   virtual ~Trash() {}
  25. };
  26.  
  27. class Aluminum : public Trash {
  28.   static float val;
  29. public:
  30.   Aluminum(float wt) : Trash(wt) {}
  31.   TrashType trashType() const { return AluminumT; }
  32.   virtual const char* name() const {
  33.     return "Aluminum";
  34.   }
  35.   float value() const { return val; }
  36.   static void value(int newval) {
  37.     val = newval;
  38.   }
  39. };
  40.  
  41. float Aluminum::val = 1.67;
  42.  
  43. class Paper : public Trash {
  44.   static float val;
  45. public:
  46.   Paper(float wt) : Trash(wt) {}
  47.   TrashType trashType() const { return PaperT; }
  48.   virtual const char* name() const {
  49.     return "Paper";
  50.   }
  51.   float value() const { return val; }
  52.   static void value(int newval) {
  53.     val = newval;
  54.   }
  55. };
  56.  
  57. float Paper::val = 0.10;
  58.  
  59. class Glass : public Trash {
  60.   static float val;
  61. public:
  62.   Glass(float wt) : Trash(wt) {}
  63.   TrashType trashType() const { return GlassT; }
  64.   virtual const char* name() const {
  65.     return "Glass";
  66.   }
  67.   float value() const { return val; }
  68.   static void value(int newval) {
  69.     val = newval;
  70.   }
  71. };
  72.  
  73. float Glass::val = 0.23;
  74.  
  75. // Sums up the value of the Trash in a bin:
  76. void sumValue(const TStack<Trash>& bin,ostream& os){
  77.   TStackIterator<Trash> tally(bin);
  78.   float val = 0;
  79.   while(tally) {
  80.     val += tally->weight() * tally->value();
  81.     os << "weight of " << tally->name()
  82.         << " = " << tally->weight() << endl;
  83.     tally++;
  84.   }
  85.   os << "Total value = " << val << endl;
  86. }
  87.  
  88. int main() {
  89.   srand(time(0)); // Seed random number generator
  90.   TStack<Trash> bin; // Default to ownership
  91.   // Fill up the Trash bin:
  92.   for(int i = 0; i < 30; i++)
  93.     switch(rand() % 3) {
  94.       case 0 :
  95.         bin.push(new Aluminum(rand() % 100));
  96.         break;
  97.       case 1 :
  98.         bin.push(new Paper(rand() % 100));
  99.         break;
  100.       case 2 :
  101.         bin.push(new Glass(rand() % 100));
  102.         break;
  103.     }
  104.   // Bins to sort into:
  105.   TStack<Trash> glassBin(0); // No ownership
  106.   TStack<Trash> paperBin(0);
  107.   TStack<Trash> alBin(0);
  108.   TStackIterator<Trash> sorter(bin);
  109.   // Sort the Trash:
  110.   // (RTTI offers a nicer solution)
  111.   while(sorter) {
  112.     // Smart pointer call:
  113.     switch(sorter->trashType()) {
  114.       case AluminumT:
  115.         alBin.push(sorter.current());
  116.         break;
  117.       case PaperT:
  118.         paperBin.push(sorter.current());
  119.         break;
  120.       case GlassT:
  121.         glassBin.push(sorter.current());
  122.         break;
  123.     }
  124.     sorter++;
  125.   }
  126.   sumValue(alBin, out);
  127.   sumValue(paperBin, out);
  128.   sumValue(glassBin, out);
  129.   sumValue(bin, out);
  130. } ///:~
  131.